package com.sgidi.einvoice.service;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.TextPosition;

import java.io.*;
import java.util.ArrayList;
import java.util.List;



    public class PdfBoxKeyWordPosition extends PDFTextStripper {
        // 关键字字符数组
        private char[] key;
        // PDF文件路径
        private String pdfPath;
        // 坐标信息集合
        private List<float[]> list = new ArrayList<float[]>();
        // 当前页信息集合
        private List<float[]> pagelist = new ArrayList<float[]>();
        // 有参构造方法
        public PdfBoxKeyWordPosition(String keyWords, String pdfPath) throws IOException {
            super();
            super.setSortByPosition(true);
            this.pdfPath = pdfPath;
            char[] key = new char[keyWords.length()];
            for (int i = 0; i < keyWords.length(); i++) {
                key[i] = keyWords.charAt(i);
            }
            this.key = key;
        }
        public char[] getKey() {
            return key;
        }
        public void setKey(char[] key) {
            this.key = key;
        }
        public String getPdfPath() {
            return pdfPath;
        }
        public void setPdfPath(String pdfPath) {
            this.pdfPath = pdfPath;
        }
        // 获取坐标信息
        public List<float[]> getCoordinate() throws IOException {
            try {
                document = PDDocument.load(new File(pdfPath));
                int pages = document.getNumberOfPages();
                for (int i = 1; i <= pages; i++) {
                    pagelist.clear();
                    super.setSortByPosition(true);
                    super.setStartPage(i);
                    super.setEndPage(i);
                    Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
                    super.writeText(document, dummy);
                    for (float[] li : pagelist) {
                        li[2] = i;
                    }
                    list.addAll(pagelist);
                }
                return list;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (document != null) {
                    document.close();
                }
            }
            return list;
        }

        private boolean _isAcross;
        private int _rotation;
        // 获取坐标信息
        @Override
        protected void writeString(String string, List<TextPosition> textPositions) throws IOException {
            for (int i = 0; i < textPositions.size(); i++) {
                // text得到pdf这一行中的汉字，同时下面有判断这一行字的长度，防止关键字在文中多次出现
                String text = textPositions.toString().replaceAll("[^\u4E00-\u9FA5]", "");
                String str = textPositions.get(i).getUnicode();
                List<TextPosition> textPositions1 = textPositions;
                if (str.equals(key[0] + "")&&text.length()<10) {
                    int count = 0;
                    for (int j = 0; j < key.length; j++) {
                        String s = "";
                        try {
                            s = textPositions.get(i + j).getUnicode();
                        } catch (Exception e) {
                            s = "";
                        }
                        if (s.equals(key[j] + "")) {
                            count++;
                        }
                    }

                    if (count == key.length) {
                        float[] idx = new float[3];
                        System.out.println("旋转角度："+ getCurrentPage().getRotation());
                        System.out.println("Width=" + getCurrentPage().getMediaBox().getWidth() + ",Height=" +getCurrentPage().getMediaBox().getHeight());
                        set_isAcross(getCurrentPage().getMediaBox().getWidth() > getCurrentPage().getMediaBox().getHeight());
                        set_rotation(getCurrentPage().getRotation());
                        System.out.println("old|x=" + textPositions.get(i).getX() + ",y=" + textPositions.get(i).getY());
                        float[] xy = getXY(textPositions.get(i).getX(), textPositions.get(i).getY(), textPositions.get(i).getPageWidth(), textPositions.get(i).getPageHeight(), getCurrentPage().getRotation());
                        idx[0] = xy[0];
                        // Y坐标 在这里减去的字体的长度，也可以直接
                        //idx[1] = textPositions.get(i).getPageHeight()-textPositions.get(i).getY();
                        idx[1] = textPositions.get(i).getPageHeight() - xy[1];
                        System.out.println("new|x=" + idx[0] + ",y=" + idx[1]);
                        pagelist.add(idx);
                    }
                }
            }
        }
        private float[] getXY(float x, float y, float width, float height, float rotation) {
            float[] xy = new float[2];
            if (rotation == 0) {
                xy[0] = x;
                xy[1] = y;
            } else if (rotation == 90) {
                xy[0] = y;
                xy[1] = width - x;
            } else if (rotation == 180) {
                xy[0] = width - x;
                xy[1] = height - y;
            } else if (rotation == 270) {
                xy[0] = height - y;
                xy[1] = x;
            }
            return xy;
        }
        public boolean is_isAcross() {
            return _isAcross;
        }

        public void set_isAcross(boolean _isAcross) {
            this._isAcross = _isAcross;
        }


        public int get_rotation() {
            return _rotation;
        }

        public void set_rotation(int _rotation) {
            this._rotation = _rotation;
        }
    }


